//
//  PageForGameOfMy.m
//  dfdsf
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "PageForGameOfMy.h"
#import "CellForMy.h"
#import "CellForCook.h"
#import "CellForNight.h"
#import <DKNightVersion.h>
#import <SDImageCache.h>
#import "CellForHead.h"
#import "WebviewForNews.h"
#import "ModelForMy.h"
#import "NetworkingHandlerOfBlock.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface PageForGameOfMy ()<UITableViewDataSource, UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *array;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) ModelForMy *model;
@property (nonatomic, retain) NSString *str;
@end

@implementation PageForGameOfMy

-(void)dealloc {
    
    [_tableView release];
    [_array release];
    [_image release];
    [_model release];
    [_str release];
    [super dealloc];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.hidesBackButton = YES;
    [self creareTableView];
    
    // 下载的图片
    //    [[NSNotificationCenter defaultCenter] addObserverForName:@"image" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
    //        NSLog(@"sss");
    //
    //        NSLog(@"%@", [note.userInfo objectForKey:@"image"]);
    //
    //
    //    }];
    //
    
    
}

- (void)creareTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor grayColor]);
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.array = @[@"检查更新", @"意见反馈", @"清理缓存", @"夜间模式", @"隐私政策", @"软件许可协议"] ;
    
    
    [self.tableView registerClass:[CellForMy class] forCellReuseIdentifier:@"poolForMy"];
    
    [self.tableView registerClass:[CellForCook class] forCellReuseIdentifier:@"poolForCook"];
    
    [self.tableView registerClass:[CellForNight class] forCellReuseIdentifier:@"poolForNight"];
    
    [self.tableView registerClass:[CellForHead class] forCellReuseIdentifier:@"poolForHead"];
    
    [self.tableView reloadData];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 5;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1 || section ==  2) {
        
        return 2;
        
    } else{
        
        return 1;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForMy *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForMy"];
    CellForCook *cell2 = [tableView dequeueReusableCellWithIdentifier:@"poolForCook"];
    
    CellForNight *cell3 = [tableView dequeueReusableCellWithIdentifier:@"poolForNight"];
    
    if (indexPath.section == 0) {
        
        CellForHead *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForHead"];
        [cell.singIn addTarget:self action:@selector(signInAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 添加点击事件换取本地照片
        [cell.heard addTarget:self action:@selector(addHeardAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.heard setImage:self.image forState:UIControlStateNormal];
        
        return cell;
        
    }else if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            
            
            cell.labelForTitle.text = self.array[0];
            
            return cell;
            
        }else {
            
            cell.labelForTitle.text = self.array[1];
            
            return cell;
            
        }
        
    }else if (indexPath.section == 2) {
        
        
        if (indexPath.row == 0) {
            
            
            cell2.labelForTitle.text = self.array[2];
            cell2.labelForNumber.text = [NSString stringWithFormat:@"%0.2f M", [[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0)];
            self.str = cell2.labelForNumber.text;
            
            return cell2;
            
        } else {
            
            cell3.labelForTitle.text = self.array[3];
            
            return cell3;
            
            
        }
        
        
    }else if (indexPath.section == 3) {
        
        
        
        cell.labelForTitle.text = self.array[4];
        
        return cell;
        
        
    } else {
        
        
        cell.labelForTitle.text = self.array[5];
        
        return cell;
        
    }
    
}

- (void)signInAction:(UIButton *) button {

    WebviewForNews *web = [[WebviewForNews alloc] init];
    web.url =@"https://open.weibo.cn/oauth2/authorize?client_id=3536949836&redirect_uri=http%3A%2F%2Fsports.sina.com.cn&scope=email%2Cdirect_messages_read%2Cdirect_messages_write%2Cfriendships_groups_read%2Cfriendships_groups_write%2Cstatuses_to_me_read%2Cfollow_app_official_microblog%2Cinvitation_write&response_type=code&version=0031205000&packagename=cn.com.sina.sports&key_hash=67c4d11e241ff6b19462172c7e559753";
    
    [self.navigationController pushViewController:web animated:NO];
    [web release];


}

- (void)addHeardAction:(UIButton *) button {
    
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    
    // 打开本地相册
    
    [self presentViewController:imagePicker animated:YES completion:^{
        // NSLog(@"打开相册");
        
        [imagePicker release];
        
    }];
    
    
    
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    // 取消
    [picker dismissViewControllerAnimated:YES completion:^{
        // NSLog(@"取消");
    }];
}


/**选取本地相片 */

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    self.image = info[UIImagePickerControllerEditedImage];
    //NSLog(@"%@",info);
    [picker dismissViewControllerAnimated:YES completion:^{
        NSLog(@"选照片");
    }];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10 * HEIGHTT;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 200 * HEIGHTT;
    } else {
        return self.view.frame.size.height / 10;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WebviewForNews *web = [[WebviewForNews alloc] init];
    
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            NSString *url = @"http://m.sina.com.cn/js/5/20120924/105.json?guid=A0000038A97F12&av=3.2.1.01&ov=4.2.2";
             [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
                
                self.model = [[ModelForMy alloc] init];
                [self.model setValuesForKeysWithDictionary:result];
                
              //  NSLog(@"%@", self.model);
                
                [_model release];
                
                
                NSString *str = @"版本信息: ";
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:[str stringByAppendingFormat:@"%@", self.model.version] preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                    web.url = self.model.url;
                    
                    [self.navigationController pushViewController:web animated:NO];
                    
                }];
                
                UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                }];
                
                [alert addAction:action];
                [alert addAction:actionCancel];
                
                [self presentViewController:alert animated:NO completion:^{
                    
                }];
                
                
            }];
            
        } else {
            
            
            web.url = @"http://dp.sina.cn/dpool/messagev2/index.php?boardid=86&vt=4&app=ty";
            [self.navigationController pushViewController:web animated:NO];
            
            
        }
    }  else  if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            
            NSString *str = @"确定清除";
            NSString *str1 = [str stringByAppendingFormat:@"%@%@", self.str, @"缓存吗?"];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清理缓存" message:str1 preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
                
                
                 [self performSelectorOnMainThread:@selector(clear) withObject:nil waitUntilDone:YES];
                
            }];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction * _Nonnull action) {
               
                
            }];
            
            [alert addAction:action1];
            [alert addAction:action];
            
            [self presentViewController:alert animated:NO completion:^{
                
            }];
            
        }
        
    }  else if (indexPath.section == 3) {
        
        web.url = @"http://pro.sina.cn/?sa=t254d1921v150&app=ty&vt=4";
        [self.navigationController pushViewController:web animated:NO];
        
    } else{
        web.url = @"http://pro.sina.cn/?sa=t254d1919v150&app=ty";
        [self.navigationController pushViewController:web animated:NO];
    }
    
    [web release];
}

- (void) clear {
    
    
    // NSLog(@"清理成功");
    
    [[SDImageCache sharedImageCache] clearDisk];
    
    CellForCook *cell2 = [self.tableView dequeueReusableCellWithIdentifier:@"poolForCook"];
    
    cell2.labelForNumber.text = [NSString stringWithFormat:@"%0.2f k", [[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0)];
    
    [self.tableView reloadData];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
